//
//  HSSKeychain.swift
//  NewsItem
//
//  Created by mac on 2023/6/29.
//  Copyright © 2023 Kaka. All rights reserved.
//

import UIKit

class HSSKeychain: NSObject {
    
    class  func saveData(){
            let key = "com.third.login"
            let value = UIDevice.current.identifierForVendor!.uuidString as NSString
           let valueData = value.data(using: String.Encoding.utf8.rawValue,allowLossyConversion: false)
            let service = Bundle.main.bundleIdentifier!
            //kSecAttrService该键值通常表示为应用bundle标识符的字符串
            //kSecAttrAccount表示存贮数据对象的键，一般是账号
            //kSecValueData该值通常是 kSecAttrAccount对应的数值
            let secItem = [
                kSecClass as NSString:kSecClassGenericPassword as String,
                kSecAttrService as String : service,
                kSecAttrAccount as String : key,
                kSecValueData as String : valueData!
                ] as NSDictionary
            var requestResult : AnyObject?
            let status = Int(SecItemAdd(secItem,&requestResult))
            switch status {
            case Int(errSecSuccess):
                print("成功")
            case Int(errSecDuplicateItem):
                print("重复插入")
            default:
                print("error")
            }
        }
    
    class func getData() -> String {
        
        let keyToSearchFor = "com.third.login"
        let service = Bundle.main.bundleIdentifier!
        let query = [
            kSecClass as NSString:kSecClassGenericPassword as String,
            kSecAttrService as String : service,
            kSecAttrAccount as String : keyToSearchFor,
            kSecReturnData as String : kCFBooleanTrue,
            ] as NSDictionary
        var valueAttributes : AnyObject?
        let results = SecItemCopyMatching(query, &valueAttributes)
        if results == Int(errSecSuccess) {
            if let resultsData = valueAttributes as? Data{
                    let value = String(data: resultsData, encoding: String.Encoding.utf8)
                    print("----------------------------    \(value ?? "")")
                    return value ?? ""
            }
        }
        return ""

    }

}
